package com.czq.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czq.dao.DishDao;
import com.czq.pojo.Dish;
import com.czq.pojo.DishDto;
import com.czq.pojo.DishFlavor;
import com.czq.service.DishService;
import com.czq.service.Dishflavor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class DishServiceImpl  extends ServiceImpl<DishDao, Dish> implements DishService {
  @Autowired
  private Dishflavor dishflavor;


    @Override
    public void savedish(DishDto dishDto) {
        super.save(dishDto); //保存dish表里的信息。
        Long dishid = dishDto.getId();  //获得dish表里的id，把这个dish_id存入到dishfalvor里面

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishid);
        }
        dishflavor.saveBatch(flavors);


    }

    @Override
    public DishDto selectbiyid(Long id) {
        Dish dish = super.getById(id);
        DishDto dto=new DishDto();
        BeanUtils.copyProperties(dish,dto);
        LambdaQueryWrapper<DishFlavor> query = new LambdaQueryWrapper<DishFlavor>();
        query.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishflavor.list(query);
        dto.setFlavors(list);
        return dto;


    }
}
